<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-input v-model="keyword" placeholder="内容"></el-input>
				</el-form-item>
				<el-form-item>
<!--          getshop 查询要重新回到第一页 再查询-->
					<el-button type="primary" v-on:click="querryKeyword">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

<!--    @selection-change="selsChange" 批量删除选择谁就调用selsChange方法把行对象赋值给数组-->
    <!--列表-->
    <el-table :data="systemdictionarydetail" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="字典明细名称" width="250" sortable>
			</el-table-column>
			<el-table-column prop="systemDictionaryType.name" label="字典明细类型" width="250"  sortable>
			</el-table-column>
			<el-table-column label="操作" >
				<template scope="scope">
					<el-button size="small" @click="handlesave(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
<!--    :disabled="this.sels.length===0" 如果没有选择批量删除就不能点击批量删除-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="PatchRemove" :disabled="this.sels.length===0">批量删除</el-button>
<!--      分页栏-->
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="currentPage" :page-size="pageSize"  :total="totals" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增和修改界面-->
<!--    :visible.sync="saveFormVisible" 模态框-->
<!--    :model="saveForm" 绑定数据-->
<!--    :rules="saveFormRules" - 表单的验证规则-->
<!--    prop="username" 绑定行类表单验证-->
		<el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false" >
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
				<el-form-item label="字典明细名称" prop="name" width="200">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
				</el-form-item>
        <el-form-item label="字典明细类型" prop="types_id" width="200">
          <!--       prop="admin_id"   v-model="saveForm.employee_id" 当前行显示的值 和 :value="item.id"配对成功
                         会显示 item.id 对应行对象的 item.name"
          -->
          <el-select v-model="saveForm.types_id" placeholder="请选择">
            <el-option
                v-for="item in systemdictionarytype"
                :label="item.name"
                :value="item.id">
              <!--              主显示-->
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
			  //列表绑定数据
        systemdictionarydetail: [],
        listLoading: false,
        currentPage:1,
        pageSize:5,
        totals:50,
        // 高级查询列表框初始为空字符串
        keyword:"",
        sels: [],//列表选中列


				saveFormVisible: false,//编辑界面是否显示
				saveLoading: false,
        saveFormRules: {
          name: [
						{ required: true, message: '请输入字典名称名称', trigger: 'blur' }
					],
          types_id: [
            { required: true, message: '请选择字典明细类型', trigger: 'blur' }
          ]
				},
				//编辑界面数据
				saveForm: {
          id:null,
          name:'',
          types_id:''
				},
        title:'',
        //店铺管理员
        systemdictionarytype:[]
			}
		},
		methods: {
		  //页面变化事件
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getsystemdictionarydetail();
			},
			//获取用户列表
      getsystemdictionarydetail() {
				let para = {
          currentPage: this.currentPage,
					pageSize: this.pageSize,
          keyword:this.keyword
				};
				this.listLoading = true;
				//NProgress.start();
				this.$http.post("/systemdictionarydetail",para).then((res) => {
					this.totals = res.data.totals;
					this.systemdictionarydetail = res.data.data;
					this.listLoading = false;
				});
			},
      //高级查询查询点击事件
      querryKeyword(){
			  //将页面置为第一页
        this.currentPage=1
        //调用查询高级查询输入框的内容,和分页数据
        this.getsystemdictionarydetail();
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/systemdictionarydetail/"+row.id).then((res) => {
						this.listLoading = false;
						//删除3个点,是否为最后一页,是否为最后一条数据,不是第一页
            let last=Math.ceil(this.totals/this.pageSize)
						if(this.currentPage!=1 && last==this.currentPage && (this.totals-1)%this.pageSize==0){
						    this.currentPage=this.currentPage-1
                this.getsystemdictionarydetail();
            }
					if (res.data.success){
					  this.$message.success("删除成功")
          }else {
					  this.$message.error("删除失败500")
          }
						this.getsystemdictionarydetail();
					});
				}).catch(() => {
          this.$message.error("删除失败400")
				});
			},
			//显示修改界面
			handlesave: function (index, row) {
        //修改  模态框回显部门名字
        this.getsystemdictionarytypes();
        this.title="修改";
				this.saveFormVisible = true;//打开模态框
				this.saveForm = Object.assign({}, row);//.assign把对象拷贝一份给新数字,把对象赋值给新数组然后再回显
        this.$refs.saveForm.clearValidate();
			},
			//显示新增界面
			handleAdd: function () {
			  //新增   模态框回显部门名字
        this.getsystemdictionarytypes();
        this.title="新增";
				this.saveFormVisible = true;
				//点击新增重置模态框
				this.saveForm = {
          id:null,
          name:'',
          types_id:''
				};
        this.$refs.saveForm.clearValidate();
			},
			//新增和修改
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {//表单验证
					if (valid) {//验证成功true
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
						  this.saveFormVisible=false;//确认提交关闭模态框
							this.saveLoading = true;//打开盲等框
							//NProgress.start();
							let para = Object.assign({}, this.saveForm);//获取表单对象
							this.$http.put("/systemdictionarydetail",para).then(result=>{
                if(result.data.success){
                  this.$message.success("操作成功")
                  this.getsystemdictionarydetail();//刷新界面
                }
              }).catch(result=>{
                this.$message.error("操作失败500")
              })
              this.saveLoading = false;//成功关闭盲等框
						}).catch(()=>{
              this.$message.error("操作失败400")
            });
					}
				});
			},
      //批量删除点击谁就把那一行的对象赋值给sels
			selsChange: function (sels) {
				this.sels = sels;
				console.log(this.sels)
			},
			//批量删除
			PatchRemove: function () {
			  //只拿id   将每一行id装成数组
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					this.$http.patch("/systemdictionarydetail",ids).then((res) => {
						this.listLoading = false;
						if (res.data.success){
						  this.$message.success("删除成功")
            }else {
						  this.$message.success("删除失败500")
            }
						this.currentPage=1;
						this.getsystemdictionarydetail();
					});
				}).catch(() => {
				});
			},
      //新增回显部门名字
      getsystemdictionarytypes(){
        this.$http.get("/systemdictionarytype").then(result=>{
          this.systemdictionarytype=result.data
        })
      }
		},
    //钩子方法页面加载完成就调用查询所有方法
		mounted() {
			this.getsystemdictionarydetail();
		}
	}

</script>

<style scoped>

</style>